import pojo.User;

import java.util.*;
import java.util.stream.Collectors;

public class StreamTest {

    public static void main(String[] args) {
        List<User> userList = generateUserData();
        //collect收集器
//        groupingByTest(userList);
//        joiningTest(userList);
//        minByTest(userList);
//        summingInt(userList);
//        countingTest(userList);
//        toSetTest(userList);
//        toMapTest(userList);
//        toListTest(userList);
        //终结操作符
//        allMatchTest(userList);
//        anyMatchTest(userList);
//        minTest(userList);
//        maxTest(userList);
//        sumTest(userList);
//        countTest(userList);
//        findFirstTest(userList);
        //中间操作符
//        peekTest(userList);
//        skipTest(userList);
//        limitTest(userList);
//        sortedTest(userList);
//        distinctTest(userList);
//        mapTest(userList);
    }



    /**
     * groupingBy
     * @param userList
     */
    public static void groupingByTest(List<User> userList) {
        Map<Integer, List<User>> collect = userList.stream().collect(Collectors.groupingBy(User::getAge));
        System.out.println(collect);
    }

    /**
     * joining分隔符
     * @param userList
     */
    public static void joiningTest(List<User> userList) {
        String str = userList.stream().map(User::getName).collect(Collectors.joining(","));
        System.out.println(str);
    }

    /**
     * minBy找最小值
     * @param userList
     */
    public static void minByTest(List<User> userList) {
        User user = userList.stream().collect(Collectors.minBy(Comparator.comparing(User::getAge))).get();
        System.out.println(user);
    }

    /**
     * summingInt
     * @param userList
     */
    public static void summingInt(List<User> userList) {
        Integer sum = userList.stream().collect(Collectors.summingInt(User::getAge));
        System.out.println(sum);
    }

    /**
     * couting总计条数
     * @param userList
     */
    public static void countingTest(List<User> userList) {
        Long count = userList.stream().collect(Collectors.counting());
        System.out.println(count);
    }

    /**
     * toSet把结果转成Set集合
     * @param userList
     */
    public static void toSetTest(List<User> userList) {
        Set<Integer> collect = userList.stream().map(User::getAge).collect(Collectors.toSet());
        System.out.println(collect);
    }

    /**
     * toMap转成Map
     * @param userList
     */
    public static void toMapTest(List<User> userList) {
        Map<String, Integer> map = userList.stream().collect(Collectors.toMap(User::getName, User::getAge));
        System.out.println(map);
    }

    /**
     * toList收集转成List集合
     * @param userList
     */
    public static void toListTest(List<User> userList) {
        userList.stream().map(User::getAge).collect(Collectors.toList()).forEach(System.out::println);
    }


    /**
     * allMatchTest集合中是否全部元素都符合条件
     * @param userList
     */
    public static void allMatchTest(List<User> userList) {
        boolean flag = userList.stream().allMatch(item -> item.getAge() == 30);
        System.out.println(flag);
    }


    /**
     * anyMatch集合中是否有元素满足条件，返回布尔值
     * @param userList
     */
    public static void anyMatchTest(List<User> userList) {
        boolean flag = userList.stream().anyMatch(item -> item.getAge() == 30);
        System.out.println(flag);
    }

    /**
     * min找最小值
     * @param userList
     */
    public static void minTest(List<User> userList) {
        User user = userList.stream().min(Comparator.comparing(User::getAge)).get();
        System.out.println(user);
    }

    /**
     * max找最大值
     * @param userList
     */
    public static void maxTest(List<User> userList) {
        User user = userList.stream().max(Comparator.comparing(User::getAge)).get();
        System.out.println(user);
    }

    /**
     * sum求和
     * @param userList
     */
    public static void sumTest(List<User> userList) {
        int sum = userList.stream().mapToInt(User::getAge).sum();
        System.out.println(sum);
    }

    /**
     * count统计元素个数
     * @param userList
     */
    public static void countTest(List<User> userList) {
        long count = userList.stream().count();
        System.out.println(count);
    }

    /**
     * findFirst拿第一个元素
     * @param userList
     */
    public static void findFirstTest(List<User> userList) {
        User user = userList.stream().findFirst().get();
        System.out.println(user);
    }

    /**
     * peek循环对元素进行操作
     * @param userList
     */
    public static void peekTest(List<User> userList) {
        System.out.println("操作前");
        userList.forEach(System.out::println);
        userList = userList.stream().peek(user -> user.setAge(user.getAge()+1)).collect(Collectors.toList());
        System.out.println("操作后：");
        userList.forEach(System.out::println);
    }


    /**
     * skip跳过多少个元素
     * @param userList
     */
    public static void skipTest(List<User> userList) {
        userList.stream().skip(3).forEach(System.out::println);
    }

    /**
     * limit指定返回个数
     * @param userList
     */
    public static void limitTest(List<User> userList) {
        userList.stream().limit(3).forEach(System.out::println);
    }

    /**
     * 排序，默认是正序，如果要倒序resver
     * @param userList
     */
    public static void sortedTest(List<User> userList) {
        //正序
        userList.stream().sorted(Comparator.comparing(User::getAge)).forEach(System.out::println);

        System.out.println("-------------------------------------");
        //倒序
        userList.stream().sorted(Comparator.comparing(User::getAge).reversed()).forEach(System.out::println);
    }


    /**
     * filter过滤除自己想要的数据
     */
    public static void filterTest() {
        List<Integer> list = Arrays.asList(6, 4, 6, 1, 6);
        list.stream().filter(item -> item > 5).forEach(System.out::println);
    }

    /**
     * distinct去重
     * @param userList
     */
    public static void distinctTest(List<User> userList){
        userList.stream().map(User::getAge).distinct().forEach(System.out::println);
    }

    /**
     * map转换成新的元素
     * @param userList
     */
    public static void mapTest(List<User> userList){
        List<Integer> ageList = userList.stream().map(User::getAge).collect(Collectors.toList());
        ageList.forEach(System.out::println);
    }


    public static List<User> generateUserData() {
        List<User> list = new ArrayList<>();
        list.add(new User("亚索", 20, "男", 1000));
        list.add(new User("阿狸", 18, "女", 2000));
        list.add(new User("李青", 21, "男", 5000));
        list.add(new User("菲奥娜", 25, "女", 2500));
        list.add(new User("乐芙兰", 18, "女", 8000));
        list.add(new User("千珏", 30, "男", 10000));
        list.add(new User("千珏", 30, "男", 10000));
        return list;
    }
}
